using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.New, "PsSpecProject", SupportsShouldProcess = true)]
    public class NewPsSpecProjectCommand : PsSpecCommandBase
    {
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 50)]
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _name = null;

        [Parameter(Mandatory = false, Position = 1, ValueFromPipelineByPropertyName = true)]
        public String Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }
        private string _description = "";

        protected override void ProcessRecord()
        {
            if (!PsSpecCmdletUtils.IsValidProjectName(Name))
            {
                WriteError(new ErrorRecord(
                    new Exception(resources.InvalidProjectName),
                    "INVALID_PROJECT_NAME",
                    ErrorCategory.InvalidArgument, Name));
                return;
            }

            if (ShouldProcess(
                String.Format("{0}", ServiceUri),
                String.Format(resources.CreatePsSpecProjectShouldProcess, Name)))
            {
                try
                {
                    Service.NewProject(Name, Description);
                }
                catch (System.Web.Services.Protocols.SoapException e)
                {
                    WriteError(new ErrorRecord(e, "PROJECT_SERVICE_ERROR", ErrorCategory.InvalidOperation, Name));
                }

                // Retrieve the project to output
                GetPsSpecProjectCommand projectCommand = new GetPsSpecProjectCommand();
                projectCommand.Name = Name;
                projectCommand.ServiceUri = ServiceUri;

                WriteObject(projectCommand.Invoke(), true);
            }
        }
    }
}
